<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
 */

class comment_api{
	
	public function getTotalCount(){
		if($rs = pcache('r','comment_count')){
			return $rs['number'];
		}
		$commentdb = load_db('comment');
		$rs['number'] = $commentdb->comment_count();
		$setting = load_api('setting');
		$cachetime = $setting->getCacheTime();
		pcache('w','comment_count',$rs,$cachetime?$cachetime['cachetime_count']:3600);
		return $rs['number'];
	}
	
	public function getcommentsbyshare($sid,$page,$pagesize){
		$conditions['share_id'] = $sid;
		return load_db('comment')->search($conditions,$page,$pagesize);
	}

	public function addcomment($data){
		$db = load_db('comment');
		if($commentid = $db->create($data)){
			$conditions['share_id'] = $data['share_id'];
			$comments = $db->search_no_page($conditions,' ptx_comment.comment_id,ptx_comment.share_id,ptx_comment.comment_txt,ptx_comment.create_time,user.user_id,user.nickname ',null,5);
			$total_comments = $db->findCount($conditions);
			load_api('share')->afterAddComment($data['share_id'],$total_comments,$comments);
			return $commentid;
		}
		return false;
	}
	
	public function delcomment($data){
		$db = load_db('comment');
		if($db->del_one($data)){
			$conditions['share_id'] = $data['share_id'];
			$comments = $db->search_no_page($conditions,' ptx_comment.comment_id,ptx_comment.share_id,ptx_comment.comment_txt,ptx_comment.create_time,user.user_id,user.nickname ',null,5);
			$total_comments = $db->findCount($conditions);
			return load_api('share')->updateshare($data['share_id'],array('total_comment'=>$total_comments,'comments'=>serialize($comments)));
		}
		return false;
	}
	
}
